package flappy.bird.cover.ui;

import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;

import flappy.bird.cover.resource.AppResource;

public class Menu extends GameCanvas implements Runnable {
	private Main midlet;
	private Graphics g;
	private int ii = 0;

	protected Menu(Main midlet) {
		super(true);
		this.midlet = midlet;
		setFullScreenMode(true);
	}

	public void start() {
		Thread t = new Thread(this);
		t.start();
	}

	public void paint(Graphics g) {
		g = getGraphics();
		int width = getWidth();
		int height = getHeight();
		g.fillRect(0, 0, getWidth(), getHeight());
		g.drawImage(AppResource.background, 0, getHeight(), Graphics.BOTTOM
				| Graphics.LEFT);

		g.drawImage(AppResource.background_bottom, 0, 290, Graphics.LEFT
				| Graphics.TOP);

		g.drawImage(AppResource.play, (getWidth() - 114) / 2,
				(getHeight() - 124) / 2, Graphics.TOP | Graphics.LEFT);

		g.drawImage(AppResource.about, (getWidth() - 60) / 2,
				(getHeight() - 34) / 2 + 100, Graphics.LEFT | Graphics.TOP);
		g.drawImage(AppResource.exit, (getWidth() - 60) / 2 + 70,
				(getHeight() - 34) / 2 + 100, Graphics.LEFT | Graphics.TOP);
		g.drawImage(AppResource.help, (getWidth() - 60) / 2 - 70,
				(getHeight() - 34) / 2 + 100, Graphics.LEFT | Graphics.TOP);
		g.drawImage(AppResource.logo, (getWidth() - 197) / 2,
				10, Graphics.TOP | Graphics.LEFT);
		flushGraphics();
	}

	protected void pointerPressed(int x, int y) {
		Display display = Display.getDisplay(midlet);
		GameUI gameUI = new GameUI();
		display.setCurrent(gameUI);
		gameUI.start();
	}

	public void render(Graphics g, int k) {
		int width = getWidth();
		int height = getHeight();
		Image tmp;
		tmp = Image.createImage(AppResource.splitted[k], 0, 0,
				AppResource.splitted[k].getWidth(),
				AppResource.splitted[k].getHeight(), 0);
		g.drawImage(tmp, (getWidth() - 114) / 2 + (114 - 32) / 2,
				(getHeight() - 124) / 2 - 20, Graphics.TOP | Graphics.LEFT);
	}

	public void run() {

		int k = 0;
		Graphics g = getGraphics();
		while (true) {
			render(g, k);
			ii++;
			if (ii >= 2) {
				k++;
				if (k >= 3)
					k = 0;

				ii = 0;
			}
			render(g, k);
			flushGraphics();
			try {
				Thread.sleep(0);
			} catch (InterruptedException ie) {
			}
		}
	}
}
